package com.itydl.property.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import static com.itydl.property.view.ShapeView.ShapeType.Circular;
import static com.itydl.property.view.ShapeView.ShapeType.Square;
import static com.itydl.property.view.ShapeView.ShapeType.Triangle;


/**
 * 作者：${Howard} on 2018/2/4 14:41
 *
 * @version 1.0
 *          说明：58效果图形自定义View
 */
public class ShapeView extends View {

    private static String TAG = ShapeView.class.getSimpleName();

    public enum ShapeType {
        Circular,//圆形
        Square,//正方形
        Triangle//三角形
    }

    //默认图形
    private ShapeType mCurrentShape = Circular;

    private Paint mPaint;
    private Path mPath;

    public ShapeView(Context context) {
        this(context, null);
    }

    public ShapeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        //设置控件的大小就为手动设置的大小
        setMeasuredDimension(Math.min(width, height), Math.min(width, height));
    }

    /**
     * 根据当前枚举类型绘制对应图形
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mCurrentShape) {
            case Circular:
                //绘制圆形
                int center = getWidth() / 2;
                mPaint.setColor(Color.RED);
                canvas.drawCircle(center, center, center, mPaint);
                break;
            case Square:
                //绘制正方形
                mPaint.setColor(Color.BLUE);
                canvas.drawRect(0, 0, getWidth(), getWidth(), mPaint);//直接构造
                break;
            case Triangle:
                //绘制三角形
                mPaint.setColor(Color.YELLOW);
                // 画路径
                mPath.moveTo(getWidth() / 2, 0);
                mPath.lineTo(0, (float) ((getWidth() / 2) * Math.sqrt(3)));
                mPath.lineTo(getWidth(), (float) ((getWidth() / 2) * Math.sqrt(3)));
                // path.lineTo(getWidth()/2,0);
                mPath.close();// 把路径闭合
                canvas.drawPath(mPath, mPaint);
                break;
        }
    }

    /**
     * 改变形状
     */
    public void changeShape() {
        switch (mCurrentShape) {
            case Circular:
                mCurrentShape = Square;
                break;
            case Square:
                mCurrentShape = Triangle;
                break;
            case Triangle:
                mCurrentShape = Circular;
                break;
        }
        invalidate();
    }

    public ShapeType getCurrentShape() {
        return mCurrentShape;
    }
}
